﻿using Microsoft.Phone.Controls;
using System.Windows;
namespace Budgetsch.WP7.View
{
    using System;
    using System.Net;

    using Budgetsch.WP7.Navigation;
    using Budgetsch.WP7.Utils;
    using Budgetsch.WP7.ViewModel;
    using Budgetsch.WP7.WP7DataService;

    using GalaSoft.MvvmLight.Ioc;
    using GalaSoft.MvvmLight.Messaging;

    /// <summary>
    /// Description for SettingsView.
    /// </summary>
    public partial class SettingsView
    {
        /// <summary>
        /// Initializes a new instance of the SettingsView class.
        /// </summary>
        public SettingsView()
        {
            InitializeComponent();
            if (SimpleIoc.Default.GetInstance<AppSettings>().IsRemember.HasValue && SimpleIoc.Default.GetInstance<AppSettings>().IsRemember.Value)
            {
                ApplicationBar.IsVisible = true;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Messenger.Default.Send(new LoadSettingsViewMessage());
        }

        //private void publicSwitch_Checked(object sender, RoutedEventArgs e)
        //{
        //    loadingControl.IsVisible = true;
        //    loadingControl.Visibility = Visibility.Visible;
            
        //    var client = new WP7DataServiceClient { CookieContainer = SimpleIoc.Default.GetInstance<CookieContainerViewModel>().CookieContainer };
        //    client.SetPublicityCompleted += (s, ev) =>
        //    {
        //        if(ev.Error != null)
        //        {
        //            publicSwitch.IsChecked = false;
        //            SwitchToggleSwitchText(false);
        //        }else
        //        {
        //            SwitchToggleSwitchText(true);
        //        }
        //        loadingControl.IsVisible = false;
        //        loadingControl.Visibility = Visibility.Collapsed;
        //    };
        //    client.SetPublicityAsync(true);
        //}

        //private void publicSwitch_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    loadingControl.IsVisible = true;
        //    loadingControl.Visibility = Visibility.Visible;
            
        //    var client = new WP7DataServiceClient { CookieContainer = SimpleIoc.Default.GetInstance<CookieContainerViewModel>().CookieContainer };
        //    client.SetPublicityCompleted += (s, ev) =>
        //        {
        //            if (ev.Error != null)
        //            {
        //                publicSwitch.IsChecked = true;
        //                SwitchToggleSwitchText(true);
        //            }else
        //            {
        //                SwitchToggleSwitchText(false);
        //            }
        //            loadingControl.IsVisible = false;
        //            loadingControl.Visibility = Visibility.Collapsed;
        //        };
        //    client.SetPublicityAsync(false);
        //}

        private void LogOutAppButton_Click(object sender, System.EventArgs e)
        {
            SimpleIoc.Default.GetInstance<AppSettings>().IsRemember = false;
            NavigationService.Navigate(new Uri("/View/LoginView.xaml", UriKind.Relative));
        }
    }
}